package org.bot.plugin.data.event;

import com.alibaba.fastjson.JSONObject;
import org.bot.api.event.DataStorage;
import org.bot.api.event.EventName;
import org.bot.api.event.Events;
import org.bot.api.flow.FlowNode;
import org.bot.plugin.data.sqlite.Db;
import org.springframework.stereotype.Component;

/**
 * @author 沧月
 * @create 0011 2023/10/11 14:55
 * @description <判断数据是否重复>
 */
@Component
@EventName("排重")
public class DataPrimaryKey implements Events {

    @Override
    public Object exec(FlowNode node, DataStorage data) {
        String dataIndex = node.getDataIndex();
        Object fdt = data.get(dataIndex);
        if (fdt == null) {
            return true;
        }
        JSONObject row = Db.qObject("select * from primary_data_log where key = ? and flow_id = ?",
                fdt.toString(), node.getFlowId());
        if (row == null) {
            //插入一天记录
            Db.execute("insert into primary_data_log (key,flow_id) values (?,?)", fdt.toString(), node.getFlowId());
            return true;
        } else {
            return false;
        }
    }
}
